import { exam as ExamApi, paper as PaperApi } from '../../../../../config/request.js';
import {
    resources,
    excelUtil as ExcelUtil
} from '../../../../../util';


module.exports = {
    name: 'sets',
    data() {
        return {
            downloadName: '试卷列表-' + new Date().getTime() + '.xlsx',
            tempLoading: false,
            examLoading: false,
            addFormVisible: false,
            allTemplateInfo: [],
            examSelectOptions: [],
            examTypes: resources.eType,
            examForm: {
                ename: '', //试卷名称
                eremark: '', //试卷备注
                examinfo: '', //模板类型
                type: '' //试卷类型
            },
            tableData: [],
            status: {
                info: { prop: 'status', label: '试卷状态', sortable: true },
                filter: {
                    list: [{
                        text: '启用',
                        value: 1
                    }, {
                        text: '禁止',
                        value: 0
                    }],
                    multiple: false
                },
                style: { align: 'center' }
            },
            type: {
                info: { prop: 'type', label: '试卷类型', sortable: true },
                filter: {
                    list: resources.eType,
                    multiple: false
                },
                style: { align: 'center' }
            },
            fields: [{
                info: { prop: 'tname', label: '模板名称', sortable: true },
                filter: {},
                style: { align: 'center' }
            }],
            rules: {
                type: [
                    { type: 'integer', required: true, message: '请选择试卷类别', trigger: 'change' }
                ],
                examinfo: [
                    { type: 'object', required: true, message: '请选择试卷模板', trigger: 'change' }
                ]
            },
        }
    },

    methods: {
        downloadExl() {
            this.downloadName = '所有试卷-' + new Date().getTime() + '.xlsx';
            var tableData = this.tableData;
            var data = ExcelUtil.formatterData(tableData, ['tid', 'checked'], { 'eremark': '试卷备注', 'tremark': '模板备注', 'tname': '试卷名称', 'type': '试卷类型' }, {
                'status': function(status) {
                    return ['禁用', '启用', '已删除'][status];
                },
                'type': ExcelUtil.formatterEtype
            });
            ExcelUtil.downloadExl(this.downloadName, data);
        },
        /**
         * 格式化状态
         */
        formatterStatu(item) {
            return item.status == 1 ? '启动' : '禁止';
        },
        /**
         * 过滤状态
         */
        filterStatu(value, item) {
            return item.status == value;
        },

        /**
         * 过滤试卷状态
         */
        filterType(value, item) {
            return item.type == value;
        },
        /**
         * 格式化试卷状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterType(item) {
            for (let i in resources.eType) {
                if (item.type == resources.eType[i].value) {
                    return resources.eType[i].label
                }
            }
        },
        /** 提交表单 */
        submitForm(formName) {
            var tinfo = this[formName].examinfo;
            var examInfo = {
                tid: tinfo.id,
                tname: tinfo.name,
                type: this[formName].type,
                tremark: tinfo.remark,
                eremark: this[formName].eremark,
                status: 0
            };
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //添加问题
                    ExamApi.createExam.call(this, examInfo,
                        //添加成功
                        (data) => {
                            const newExam = data['exam'];
                            Object.assign(newExam, { checked: newExam.status == 1 });
                            this.tableData.push(newExam);
                            this.$message.success('添加试卷成功 ！！');
                            this.addFormVisible = false;
                            this.$refs[formName].resetFields();
                            this.examForm = {
                                ename: '', //试卷名称
                                eremark: '', //试卷备注
                                type: '', //模板类型
                                examinfo: ''
                                    // options: [] //选项                           
                            }
                        },
                        //添加失败
                        (msg) => {
                            this.$message.error('添加问题失败 ！！');
                        })
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        /** 删除 */
        onDeleteExam(exam, index) {
            this.$confirm('是否确认删除该题目？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this.tableData.splice(index, 1);
                ExamApi.deleteExam.call(this, {
                    eid: exam.eid,
                }, data => {
                    this.$message.success('删除成功');
                })
            }).catch(_ => {});
        },
        /**
         * 启用或禁用该试卷
         * 
         * @param {any} exam 
         */
        onCheckedChange(exam) {
            ExamApi.updateExam.call(this, {
                eid: exam.eid,
                status: exam.checked ? 1 : 0
            }, data => {
                exam.status = data.exam.status;
                this.$message.success(data.exam.status == 1 ? '已经启用' : '已经禁用');
            })

        },
        initData() {
            //查询所有数据
            this.getExamData();
            //查询所有模板
            this.getTemplateData();
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            // console.log('user', user);
            return user && user.level == 1;
        },
        getTemplateData() {
            var that = this;
            that.tempLoading = true;

            PaperApi.queryAll.call(this, (data) => {
                var papers = data.papers;
                papers.forEach(function(paper) {
                    that.examSelectOptions.push({ value: paper, key: paper.id, label: paper.name });
                });
                setTimeout(function() {
                    that.tempLoading = false;
                }, 500);

            })
        },
        getExamData() {
            var that = this;
            that.examLoading = true;
            ExamApi.queryAll.call(this, (data) => {
                data.exams.forEach(exam => {
                    Object.assign(exam, { checked: exam.status == 1 });
                });
                this.tableData = data.exams;
                setTimeout(function() {
                    that.examLoading = false;
                }, 500);
            });
        },
    },

    mounted: function() {
        this.initData();
    }
}